package com.tbit.uqbike.compalint;

import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.compalint.enums.PlatformEnum;
import com.tbit.uqbike.compalint.param.*;
import com.tbit.uqbike.compalint.service.base.AbstractComplaintService;
import com.tbit.uqbike.compalint.service.factory.ComplaintFactory;
import com.tbit.uqbike.compalint.service.quartz.ComplaintPullQuartz;
import com.tbit.uqbike.compalint.vo.ComplaintHistoryVO;
import com.tbit.uqbike.compalint.vo.ComplaintVO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.webmanager.controller.BaseController;
import com.tbit.uqbike.webmanager.util.UserContextUtil;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.mail.search.SearchTerm;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 平台客诉
 *
 * @author <a href="mailto:zhang.zhiliang@tbit.com.cn">Zhang ZhiLiang</a>
 * @version 1.0
 * @see <a href="http://adsl1.tbit.com.cn:21490/pages/viewpage.action?pageId=82182491">订单号规范</a>
 * @since 2025/2/14
 */
@Slf4j
@AllArgsConstructor
@RequestMapping("/complaint")
@RestController
public class ComplaintController extends BaseController {

    private final ComplaintFactory complaintFactory;
    // 不需要客诉的 品牌
    private final Set<Integer> quitBrandIdSet = new HashSet<Integer>(){{
        add(100250);
        add(100016);
    }};
    /**
     * 分页查询客诉列表
     * @param param 分页请求参数
     * @return 分页列表对象
     */
    @GetMapping("/page")
    public Result<PageResult<ComplaintVO>> page(ComplaintPageParam param){
        if (quitBrandIdSet.contains(Integer.parseInt(param.getBrandId()))){
            return Result.success();
        }
        // 分页查询
        AbstractComplaintService complaintService = complaintFactory.getComplaintService(PlatformEnum.getByCode(param.getPlatform()));
        PageResult<ComplaintVO> page = complaintService.page(param);
        return Result.success(page);
    }

    /**
     * 导出客诉列表
     * @param param 条件查询参数 忽略分页
     * @return 是否成功
     */
    @GetMapping("/export")
    @OperateLog(module = ModuleEnum.COMPALINT, methods = MethodEnum.COMPLAINT_EXPORT)
    public Result<String> export(ComplaintPageParam param){
        // 条件查询
        AbstractComplaintService complaintService = complaintFactory.getComplaintService(PlatformEnum.getByCode(param.getPlatform()));
        List<ComplaintVO> complaintVOList = complaintService.list(param);
        // 导出
        ExcelUtil<ComplaintVO> excelUtil = new ExcelUtil<>(ComplaintVO.class);
        JsonResult jsonResult = excelUtil.exportExcel(complaintVOList, "客诉列表");
        return Result.success(jsonResult.getData().toString());
    }

    /**
     * 客诉协商历史详情
     * @param param 查询参数
     * @return 客诉协商历史详情
     */
    @GetMapping("/history")
    public Result<List<ComplaintHistoryVO>> history(ComplaintHistoryParam param){
        // 查询
        AbstractComplaintService complaintService = complaintFactory.getComplaintService(PlatformEnum.getByCode(param.getPlatform()));
        List<ComplaintHistoryVO> historyVOList = complaintService.history(param);
        return Result.success(historyVOList);
    }

    /**
     * 回复客诉
     * @param param 回复客诉参数
     * @return 是否成功
     */
    @PostMapping("/response")
    @OperateLog(module = ModuleEnum.COMPALINT, methods = MethodEnum.COMPLAINT_RESPONSE)
    public Result<Boolean> Response(@RequestBody ComplaintResponseParam param, HttpServletRequest httpServletRequest) {
        LoginInfo loginInfo = getLogin(httpServletRequest);
        UserContextUtil.set(UserContextUtil.USER_KEY,loginInfo);
        // 回复
        AbstractComplaintService complaintService = complaintFactory.getComplaintService(PlatformEnum.getByCode(param.getPlatform()));
        return Result.success(complaintService.response(param));
    }

    /**
     * 拉取客诉状态校验
     * @param param 校验客诉状态参数
     * @return 是否成功
     */
    @PutMapping("/verify")
    public Result<Boolean> verify(ComplaintVerifyParam param){
        // 拉取客诉状态校验
        AbstractComplaintService complaintService = complaintFactory.getComplaintService(PlatformEnum.getByCode(param.getPlatform()));
        return Result.success(complaintService.verify(param));
    }

    /**
     * 平台主动拉取客诉
     * @param param 拉取客诉参数
     * @return 执行结果
     */
    @GetMapping("/pull")
    public Result<Boolean> pull(ComplaintPullParam param){
        AbstractComplaintService complaintService = complaintFactory.getComplaintService(PlatformEnum.getByCode(param.getPlatform()));
        return Result.success(complaintService.complaintPull(param));
    }


    @Resource
    private ComplaintPullQuartz complaintPullQuartz;

    /**
     * 主动触发一次定时任务
     * @return
     */
    @GetMapping("/schedule")
    public Result<Boolean> quartz(){
        complaintPullQuartz.pull();
        return Result.success(true);
    }
}
